package com.ldh.mod4j.businessdomain.generator.hibernate;

import com.google.inject.Inject;
import com.ldh.mod4j.businessdomain.businessDomainDsl.Association;
import com.ldh.mod4j.businessdomain.businessDomainDsl.BusinessClass;
import com.ldh.mod4j.businessdomain.businessDomainDsl.BusinessDomainModel;
import java.util.List;
import org.eclipse.xtext.xbase.lib.ComparableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.StringExtensions;
import org.eclipse.xtext.xtend2.lib.StringConcatenation;
import org.mod4j.businessdomain.generator.helpers.BusinessClassHelpers;
import org.mod4j.businessdomain.generator.helpers.BusinessDomainModelHelpers;
import org.mod4j.dslcommon.generator.helpers.NameMapper;
import org.mod4j.dslcommon.xpand.java.JavaComments;

@SuppressWarnings("all")
public class DaoImplBase {
  
  @Inject
  private JavaComments javaComments;
  
  public StringConcatenation daoImplementationBase(final BusinessClass clazz) {
    StringConcatenation _builder = new StringConcatenation();
    StringConcatenation _regenerationWarningHeader = this.javaComments.regenerationWarningHeader("Mod4j BusinessDomainDsl generator: DaoImplBase.xtend", clazz);
    _builder.append(_regenerationWarningHeader, "");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("package ");
    BusinessDomainModel _model = BusinessDomainModelHelpers.getModel(clazz);
    String _name = _model.getName();
    String _daoImplPackage = NameMapper.getDaoImplPackage(_name);
    _builder.append(_daoImplPackage, "");
    _builder.append(";");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("import java.sql.SQLException;");
    _builder.newLine();
    _builder.append("import java.util.List;");
    _builder.newLine();
    _builder.append("import java.util.Set;");
    _builder.newLine();
    _builder.append("import java.util.HashSet;");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import ");
    BusinessDomainModel _model_1 = BusinessDomainModelHelpers.getModel(clazz);
    String _name_1 = _model_1.getName();
    String _name_2 = clazz.getName();
    String _fqnDomainClass = NameMapper.getFqnDomainClass(_name_1, _name_2);
    _builder.append(_fqnDomainClass, "");
    _builder.append(";");
    _builder.newLineIfNotEmpty();
    _builder.append("import ");
    BusinessDomainModel _model_2 = BusinessDomainModelHelpers.getModel(clazz);
    String _name_3 = _model_2.getName();
    String _name_4 = clazz.getName();
    String _fqnDaoBaseInterface = NameMapper.getFqnDaoBaseInterface(_name_3, _name_4);
    _builder.append(_fqnDaoBaseInterface, "");
    _builder.append(";");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("import org.hibernate.Criteria;");
    _builder.newLine();
    _builder.append("import org.hibernate.HibernateException;");
    _builder.newLine();
    _builder.append("import org.hibernate.Session;");
    _builder.newLine();
    _builder.append("import org.hibernate.criterion.DetachedCriteria;");
    _builder.newLine();
    _builder.append("import org.hibernate.criterion.Example;");
    _builder.newLine();
    _builder.append("import org.hibernate.criterion.MatchMode;");
    _builder.newLine();
    _builder.append("import org.hibernate.criterion.Projections;");
    _builder.newLine();
    _builder.append("import org.hibernate.criterion.Restrictions;");
    _builder.newLine();
    _builder.append("import org.springframework.orm.hibernate3.HibernateCallback;");
    _builder.newLine();
    _builder.append("import org.springframework.orm.hibernate3.support.HibernateDaoSupport;");
    _builder.newLine();
    _builder.append("import org.springframework.util.Assert;");
    _builder.newLine();
    _builder.append("import org.mod4j.runtime.queries.SearchParameters;");
    _builder.newLine();
    {
      List<Association> _oneAssociationsToInHierarchy = BusinessClassHelpers.getToOneAssociationsToInHierarchy(clazz);
      for(final Association e : _oneAssociationsToInHierarchy) {
        BusinessClass _target = e.getTarget();
        final BusinessClass importClass = _target;
        _builder.newLineIfNotEmpty();
        {
          boolean _operator_notEquals = ObjectExtensions.operator_notEquals(importClass, clazz);
          if (_operator_notEquals) {
            _builder.append("import ");
            BusinessDomainModel _model_3 = BusinessDomainModelHelpers.getModel(importClass);
            String _name_5 = _model_3.getName();
            String _name_6 = importClass.getName();
            String _fqnDomainClass_1 = NameMapper.getFqnDomainClass(_name_5, _name_6);
            _builder.append(_fqnDomainClass_1, "");
            _builder.append(";");
            _builder.newLineIfNotEmpty();
            _builder.append("import ");
            BusinessDomainModel _model_4 = BusinessDomainModelHelpers.getModel(importClass);
            String _name_7 = _model_4.getName();
            String _name_8 = importClass.getName();
            String _fqnDaoBaseImplClass = NameMapper.getFqnDaoBaseImplClass(_name_7, _name_8);
            _builder.append(_fqnDaoBaseImplClass, "");
            _builder.append(";");
            _builder.newLineIfNotEmpty();
          }
        }
      }
    }
    {
      List<Association> _oneAssociationsFromInHierarchy = BusinessClassHelpers.getToOneAssociationsFromInHierarchy(clazz);
      for(final Association e_1 : _oneAssociationsFromInHierarchy) {
        BusinessClass _source = e_1.getSource();
        final BusinessClass importClass_1 = _source;
        _builder.newLineIfNotEmpty();
        {
          boolean _operator_notEquals_1 = ObjectExtensions.operator_notEquals(importClass_1, clazz);
          if (_operator_notEquals_1) {
            _builder.append("import ");
            BusinessDomainModel _model_5 = BusinessDomainModelHelpers.getModel(importClass_1);
            String _name_9 = _model_5.getName();
            String _name_10 = importClass_1.getName();
            String _fqnDomainClass_2 = NameMapper.getFqnDomainClass(_name_9, _name_10);
            _builder.append(_fqnDomainClass_2, "");
            _builder.append(";");
            _builder.newLineIfNotEmpty();
            _builder.append("import ");
            BusinessDomainModel _model_6 = BusinessDomainModelHelpers.getModel(importClass_1);
            String _name_11 = _model_6.getName();
            String _name_12 = importClass_1.getName();
            String _fqnDaoBaseImplClass_1 = NameMapper.getFqnDaoBaseImplClass(_name_11, _name_12);
            _builder.append(_fqnDaoBaseImplClass_1, "");
            _builder.append(";");
            _builder.newLineIfNotEmpty();
          }
        }
      }
    }
    _builder.newLine();
    _builder.append("/**");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("* ");
    String _name_13 = clazz.getName();
    String _javaDomainClass = NameMapper.javaDomainClass(_name_13);
    _builder.append(_javaDomainClass, " ");
    _builder.append(" Data Access Object (DAO) Implementation. Responsible for Creating,");
    _builder.newLineIfNotEmpty();
    _builder.append(" ");
    _builder.append("* Reading, Updating, Deleting and retrieving lists of ");
    String _name_14 = clazz.getName();
    String _javaDomainClass_1 = NameMapper.javaDomainClass(_name_14);
    _builder.append(_javaDomainClass_1, " ");
    _builder.append(" objects in the database.");
    _builder.newLineIfNotEmpty();
    _builder.append(" ");
    _builder.append("* All database access is done here.");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("*  ");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("* @author Generated by Mod4j");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("*/");
    _builder.newLine();
    _builder.newLine();
    _builder.append("public class ");
    String _name_15 = clazz.getName();
    String _javaDaoBaseClass = NameMapper.javaDaoBaseClass(_name_15);
    _builder.append(_javaDaoBaseClass, "");
    _builder.append(" extends HibernateDaoSupport implements ");
    String _name_16 = clazz.getName();
    String _javaDaoBaseInterface = NameMapper.javaDaoBaseInterface(_name_16);
    _builder.append(_javaDaoBaseInterface, "");
    _builder.append(" {");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("    ");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("* {@inheritDoc}");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("public ");
    String _name_17 = clazz.getName();
    String _javaDomainClass_2 = NameMapper.javaDomainClass(_name_17);
    _builder.append(_javaDomainClass_2, "    ");
    _builder.append(" retrieve(Long id) {");
    _builder.newLineIfNotEmpty();
    _builder.append("        ");
    _builder.append("Object o = getHibernateTemplate().get(");
    String _name_18 = clazz.getName();
    String _javaDomainClass_3 = NameMapper.javaDomainClass(_name_18);
    _builder.append(_javaDomainClass_3, "        ");
    _builder.append(".class, id);");
    _builder.newLineIfNotEmpty();
    _builder.append("        ");
    _builder.append("if (o == null) {");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("return null;");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("return (");
    String _name_19 = clazz.getName();
    String _javaDomainClass_4 = NameMapper.javaDomainClass(_name_19);
    _builder.append(_javaDomainClass_4, "        ");
    _builder.append(") o;");
    _builder.newLineIfNotEmpty();
    _builder.append("    ");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("    ");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("* {@inheritDoc}");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("public Long add(");
    String _name_20 = clazz.getName();
    String _javaDomainClass_5 = NameMapper.javaDomainClass(_name_20);
    _builder.append(_javaDomainClass_5, "    ");
    _builder.append(" object) {");
    _builder.newLineIfNotEmpty();
    _builder.append("        ");
    _builder.append("Long result = (Long) getHibernateTemplate().save(object);");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("return result;");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("    ");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("* {@inheritDoc}");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("public void delete(");
    String _name_21 = clazz.getName();
    String _javaDomainClass_6 = NameMapper.javaDomainClass(_name_21);
    _builder.append(_javaDomainClass_6, "    ");
    _builder.append(" object ) {");
    _builder.newLineIfNotEmpty();
    _builder.append("        ");
    _builder.append("getHibernateTemplate().delete(object);");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("    ");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("* {@inheritDoc}");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("@SuppressWarnings(\"unchecked\")");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("public List<");
    String _name_22 = clazz.getName();
    String _javaDomainClass_7 = NameMapper.javaDomainClass(_name_22);
    _builder.append(_javaDomainClass_7, "    ");
    _builder.append("> listAll() {");
    _builder.newLineIfNotEmpty();
    _builder.append("        ");
    _builder.append("return getHibernateTemplate().find(\"from ");
    String _name_23 = clazz.getName();
    String _javaDomainClass_8 = NameMapper.javaDomainClass(_name_23);
    _builder.append(_javaDomainClass_8, "        ");
    _builder.append("\");");
    _builder.newLineIfNotEmpty();
    _builder.append("    ");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("    ");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("* {@inheritDoc}");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("@SuppressWarnings(\"unchecked\")");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("public List<");
    String _name_24 = clazz.getName();
    String _javaDomainClass_9 = NameMapper.javaDomainClass(_name_24);
    _builder.append(_javaDomainClass_9, "    ");
    _builder.append("> listPage(final int firstResult, final int maxResults) {");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("        ");
    _builder.append("DetachedCriteria criteria = DetachedCriteria.forClass(");
    String _name_25 = clazz.getName();
    String _javaDomainClass_10 = NameMapper.javaDomainClass(_name_25);
    _builder.append(_javaDomainClass_10, "        ");
    _builder.append(".class);");
    _builder.newLineIfNotEmpty();
    _builder.append("        ");
    _builder.append("return getHibernateTemplate().findByCriteria(criteria, firstResult, maxResults);");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("}  ");
    _builder.newLine();
    _builder.append("    ");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("* {@inheritDoc}");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("@SuppressWarnings(\"unchecked\")");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("public List<");
    String _name_26 = clazz.getName();
    String _javaDomainClass_11 = NameMapper.javaDomainClass(_name_26);
    _builder.append(_javaDomainClass_11, "    ");
    _builder.append("> listPage(final int firstResult, final int maxResults,");
    _builder.newLineIfNotEmpty();
    _builder.append("        ");
    _builder.append("final String sortProperty, final boolean isAscending) {");
    _builder.newLine();
    _builder.append("        ");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("Assert.notNull(sortProperty, \"argument [sortProperty] may not be null\");");
    _builder.newLine();
    _builder.append("        ");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("org.hibernate.criterion.Order order;");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("if (isAscending) {");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("order = org.hibernate.criterion.Order.asc(sortProperty);");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("} else {");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("order = org.hibernate.criterion.Order.desc(sortProperty);");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("DetachedCriteria criteria = DetachedCriteria.forClass(");
    String _name_27 = clazz.getName();
    String _javaDomainClass_12 = NameMapper.javaDomainClass(_name_27);
    _builder.append(_javaDomainClass_12, "        ");
    _builder.append(".class).addOrder(order);");
    _builder.newLineIfNotEmpty();
    _builder.append("        ");
    _builder.append("return getHibernateTemplate().findByCriteria(criteria, firstResult, maxResults);");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("    ");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("* {@inheritDoc}");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("@SuppressWarnings(\"unchecked\")");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("public List<");
    String _name_28 = clazz.getName();
    String _javaDomainClass_13 = NameMapper.javaDomainClass(_name_28);
    _builder.append(_javaDomainClass_13, "    ");
    _builder.append("> listPageByExample(final ");
    String _name_29 = clazz.getName();
    String _javaDomainClass_14 = NameMapper.javaDomainClass(_name_29);
    _builder.append(_javaDomainClass_14, "    ");
    _builder.append(" example, final int firstResult, final int maxResults,");
    _builder.newLineIfNotEmpty();
    _builder.append("            ");
    _builder.append("final String sortProperty, final boolean isAscending) {");
    _builder.newLine();
    _builder.append("            ");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("Assert.notNull(example, \"argument [example] may not be null\");");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("Assert.notNull(sortProperty, \"argument [sortProperty] may not be null\");");
    _builder.newLine();
    _builder.append("        ");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("List<");
    String _name_30 = clazz.getName();
    String _javaDomainClass_15 = NameMapper.javaDomainClass(_name_30);
    _builder.append(_javaDomainClass_15, "        ");
    _builder.append("> result;");
    _builder.newLineIfNotEmpty();
    _builder.append("        ");
    _builder.append("result = getHibernateTemplate().executeFind(new HibernateCallback() {");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("public Object doInHibernate(final Session session) throws HibernateException, SQLException {");
    _builder.newLine();
    _builder.newLine();
    _builder.append("                ");
    _builder.append("org.hibernate.criterion.Order order;");
    _builder.newLine();
    _builder.append("                ");
    _builder.append("if (isAscending) {");
    _builder.newLine();
    _builder.append("                    ");
    _builder.append("order = org.hibernate.criterion.Order.asc(sortProperty);");
    _builder.newLine();
    _builder.append("                ");
    _builder.append("} else {");
    _builder.newLine();
    _builder.append("                    ");
    _builder.append("order = org.hibernate.criterion.Order.desc(sortProperty);");
    _builder.newLine();
    _builder.append("                ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("                ");
    _builder.append("return session.createCriteria(");
    String _name_31 = clazz.getName();
    String _javaDomainClass_16 = NameMapper.javaDomainClass(_name_31);
    _builder.append(_javaDomainClass_16, "                ");
    _builder.append(".class).add(");
    _builder.newLineIfNotEmpty();
    _builder.append("                        ");
    _builder.append("Example.create(example).enableLike(MatchMode.ANYWHERE).ignoreCase())");
    _builder.newLine();
    _builder.append("                        ");
    _builder.append(".setFirstResult(firstResult).setMaxResults(maxResults).addOrder(order).list();");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("});");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("return result;");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("    ");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("* {@inheritDoc}");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("public long count() {");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("return ((Long) getHibernateTemplate().iterate(\"select count(*) from ");
    String _name_32 = clazz.getName();
    String _javaDomainClass_17 = NameMapper.javaDomainClass(_name_32);
    _builder.append(_javaDomainClass_17, "        ");
    _builder.append("\").next()).longValue();");
    _builder.newLineIfNotEmpty();
    _builder.append("    ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("    ");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("* {@inheritDoc}");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("@SuppressWarnings(\"unchecked\")");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("public long count(final ");
    String _name_33 = clazz.getName();
    String _javaDomainClass_18 = NameMapper.javaDomainClass(_name_33);
    _builder.append(_javaDomainClass_18, "    ");
    _builder.append(" example) {");
    _builder.newLineIfNotEmpty();
    _builder.append("    ");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("Assert.notNull(example, \"argument [example] may not be null\");");
    _builder.newLine();
    _builder.newLine();
    _builder.append("        ");
    _builder.append("List<Integer> result;");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("result = getHibernateTemplate().executeFind(new HibernateCallback() {");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("public Object doInHibernate(final Session session)");
    _builder.newLine();
    _builder.append("                    ");
    _builder.append("throws HibernateException, SQLException {");
    _builder.newLine();
    _builder.append("                ");
    _builder.append("Criteria crit = session.createCriteria(");
    String _name_34 = clazz.getName();
    String _javaDomainClass_19 = NameMapper.javaDomainClass(_name_34);
    _builder.append(_javaDomainClass_19, "                ");
    _builder.append(".class);");
    _builder.newLineIfNotEmpty();
    _builder.append("                ");
    _builder.append("crit.setProjection(Projections.rowCount());");
    _builder.newLine();
    _builder.append("                ");
    _builder.append("crit.add(Example.create(example).enableLike(MatchMode.ANYWHERE).ignoreCase());");
    _builder.newLine();
    _builder.append("                ");
    _builder.append("return crit.list();");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("});");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("return result.get(0);");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("    ");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("* {@inheritDoc}");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("@SuppressWarnings(\"unchecked\")");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("public long countByExample(final ");
    String _name_35 = clazz.getName();
    String _javaDomainClass_20 = NameMapper.javaDomainClass(_name_35);
    _builder.append(_javaDomainClass_20, "    ");
    _builder.append(" example) {");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("        ");
    _builder.append("Assert.notNull(example, \"argument [example] may not be null\");");
    _builder.newLine();
    _builder.newLine();
    _builder.append("        ");
    _builder.append("final SearchParameters parameters = new SearchParameters();");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("parameters.setMatchMode(MatchMode.ANYWHERE);");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("parameters.setIgnoreCase(Boolean.TRUE);");
    _builder.newLine();
    _builder.newLine();
    _builder.append("        ");
    _builder.append("List<Integer> result;");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("result = getHibernateTemplate().executeFind(new ");
    String _name_36 = clazz.getName();
    String _javaDomainClass_21 = NameMapper.javaDomainClass(_name_36);
    _builder.append(_javaDomainClass_21, "        ");
    _builder.append("HibernateCallback(example, parameters, true));");
    _builder.newLineIfNotEmpty();
    _builder.append("        ");
    _builder.append("return result.get(0);");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("    ");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("* {@inheritDoc}");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("@SuppressWarnings(\"unchecked\")");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("public long countByExample(final ");
    String _name_37 = clazz.getName();
    String _javaDomainClass_22 = NameMapper.javaDomainClass(_name_37);
    _builder.append(_javaDomainClass_22, "    ");
    _builder.append(" example, final SearchParameters parameters) {");
    _builder.newLineIfNotEmpty();
    _builder.append("  ");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("if (parameters == null) {");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("return countByExample(example);");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("        ");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("Assert.notNull(example, \"argument [example] may not be null\");");
    _builder.newLine();
    _builder.newLine();
    _builder.append("        ");
    _builder.append("List<Integer> result;");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("result = getHibernateTemplate().executeFind(new ");
    String _name_38 = clazz.getName();
    String _javaDomainClass_23 = NameMapper.javaDomainClass(_name_38);
    _builder.append(_javaDomainClass_23, "        ");
    _builder.append("HibernateCallback(example, parameters, true));");
    _builder.newLineIfNotEmpty();
    _builder.append("        ");
    _builder.append("return result.get(0);");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("    ");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("* {@inheritDoc}");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("@SuppressWarnings(\"unchecked\")");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("public List<");
    String _name_39 = clazz.getName();
    String _javaDomainClass_24 = NameMapper.javaDomainClass(_name_39);
    _builder.append(_javaDomainClass_24, "    ");
    _builder.append("> findByExample(final ");
    String _name_40 = clazz.getName();
    String _javaDomainClass_25 = NameMapper.javaDomainClass(_name_40);
    _builder.append(_javaDomainClass_25, "    ");
    _builder.append(" example) {");
    _builder.newLineIfNotEmpty();
    _builder.append("        ");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("Assert.notNull(example, \"argument [example] may not be null\");");
    _builder.newLine();
    _builder.append("        ");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("SearchParameters parameters = new SearchParameters();");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("parameters.setMatchMode(MatchMode.ANYWHERE);");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("parameters.setIgnoreCase(Boolean.TRUE);");
    _builder.newLine();
    _builder.append("        ");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("List<");
    String _name_41 = clazz.getName();
    String _javaDomainClass_26 = NameMapper.javaDomainClass(_name_41);
    _builder.append(_javaDomainClass_26, "        ");
    _builder.append("> result;");
    _builder.newLineIfNotEmpty();
    _builder.append("        ");
    _builder.append("result = getHibernateTemplate().executeFind(new ");
    String _name_42 = clazz.getName();
    String _javaDomainClass_27 = NameMapper.javaDomainClass(_name_42);
    _builder.append(_javaDomainClass_27, "        ");
    _builder.append("HibernateCallback(example, parameters));");
    _builder.newLineIfNotEmpty();
    _builder.append("        ");
    _builder.append("return result;");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("    ");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("* {@inheritDoc}");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("@SuppressWarnings(\"unchecked\")");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("public List<");
    String _name_43 = clazz.getName();
    String _javaDomainClass_28 = NameMapper.javaDomainClass(_name_43);
    _builder.append(_javaDomainClass_28, "    ");
    _builder.append("> findByExample(final ");
    String _name_44 = clazz.getName();
    String _javaDomainClass_29 = NameMapper.javaDomainClass(_name_44);
    _builder.append(_javaDomainClass_29, "    ");
    _builder.append(" example, final SearchParameters parameters) {");
    _builder.newLineIfNotEmpty();
    _builder.append("        ");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("if (parameters == null) {");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("return findByExample(example);");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("        ");
    _builder.append("Assert.notNull(example, \"argument [example] may not be null\");");
    _builder.newLine();
    _builder.newLine();
    _builder.append("        ");
    _builder.append("List<");
    String _name_45 = clazz.getName();
    String _javaDomainClass_30 = NameMapper.javaDomainClass(_name_45);
    _builder.append(_javaDomainClass_30, "        ");
    _builder.append("> result;");
    _builder.newLineIfNotEmpty();
    _builder.append("        ");
    _builder.append("result = getHibernateTemplate().executeFind(new ");
    String _name_46 = clazz.getName();
    String _javaDomainClass_31 = NameMapper.javaDomainClass(_name_46);
    _builder.append(_javaDomainClass_31, "        ");
    _builder.append("HibernateCallback(example, parameters));");
    _builder.newLineIfNotEmpty();
    _builder.append("        ");
    _builder.append("return result;");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    {
      List<Association> _allToOneAssociationsInHierarchy = BusinessClassHelpers.getAllToOneAssociationsInHierarchy(clazz);
      int _size = _allToOneAssociationsInHierarchy.size();
      boolean _operator_greaterThan = ComparableExtensions.<Integer>operator_greaterThan(((Integer)_size), ((Integer)0));
      if (_operator_greaterThan) {
        _builder.append("\t");
        _builder.append("/**");
        _builder.newLine();
        _builder.append("\t");
        _builder.append(" ");
        _builder.append("* Composes the given <code>criteria</code> object for associations of the given <code>example</code>.");
        _builder.newLine();
        _builder.append("\t");
        _builder.append(" ");
        _builder.append("* Adds an example object for each of the to-one associations of the <code>example</code> object,");
        _builder.newLine();
        _builder.append("\t");
        _builder.append(" ");
        _builder.append("* or, if the id is set, adds a restriction based on the id. All eventually existing to-many associations of");
        _builder.newLine();
        _builder.append("\t");
        _builder.append(" ");
        _builder.append("* <code>example</code> object are ignored.");
        _builder.newLine();
        _builder.append("\t");
        _builder.append(" ");
        _builder.append("* The <code>addedAssociations</code> set is used to prevent looping in circular associations.");
        _builder.newLine();
        _builder.append("\t");
        _builder.append(" ");
        _builder.append("* ");
        _builder.newLine();
        _builder.append("\t");
        _builder.append(" ");
        _builder.append("* @param criteria");
        _builder.newLine();
        _builder.append("\t");
        _builder.append(" ");
        _builder.append("* @param example");
        _builder.newLine();
        _builder.append("\t");
        _builder.append(" ");
        _builder.append("* @param associationPath");
        _builder.newLine();
        _builder.append("\t");
        _builder.append(" ");
        _builder.append("* @param parameters");
        _builder.newLine();
        _builder.append("\t");
        _builder.append(" ");
        _builder.append("* @param addedAssociations");
        _builder.newLine();
        _builder.append("\t");
        _builder.append(" ");
        _builder.append("*/");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("static void addAssociatedExampleCriteria(Criteria criteria, final ");
        String _name_47 = clazz.getName();
        String _javaDomainClass_32 = NameMapper.javaDomainClass(_name_47);
        _builder.append(_javaDomainClass_32, "	");
        _builder.append(" example, final String associationPath,");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append(" \t");
        _builder.append("final SearchParameters parameters, final Set<Object> addedAssociations) {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append(" ");
        _builder.newLine();
        {
          List<Association> _oneAssociationsToInHierarchy_1 = BusinessClassHelpers.getToOneAssociationsToInHierarchy(clazz);
          for(final Association e_2 : _oneAssociationsToInHierarchy_1) {
            _builder.append("\t\t    ");
            _builder.append("if (example.get");
            String _targetRoleName = e_2.getTargetRoleName();
            String _firstUpper = StringExtensions.toFirstUpper(_targetRoleName);
            _builder.append(_firstUpper, "		    ");
            _builder.append("() != null &&!addedAssociations.contains(example.get");
            String _targetRoleName_1 = e_2.getTargetRoleName();
            String _firstUpper_1 = StringExtensions.toFirstUpper(_targetRoleName_1);
            _builder.append(_firstUpper_1, "		    ");
            _builder.append("())) {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t    ");
            _builder.append("            ");
            _builder.newLine();
            _builder.append("\t\t    ");
            _builder.append("    ");
            BusinessClass _target_1 = e_2.getTarget();
            String _name_48 = _target_1.getName();
            String _javaDomainClass_33 = NameMapper.javaDomainClass(_name_48);
            _builder.append(_javaDomainClass_33, "		        ");
            _builder.append(" associatedExample = example.get");
            String _targetRoleName_2 = e_2.getTargetRoleName();
            String _firstUpper_2 = StringExtensions.toFirstUpper(_targetRoleName_2);
            _builder.append(_firstUpper_2, "		        ");
            _builder.append("();");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t    ");
            _builder.append("    ");
            _builder.append("String ");
            String _targetRoleName_3 = e_2.getTargetRoleName();
            _builder.append(_targetRoleName_3, "		        ");
            _builder.append("Path = associationPath + \"");
            String _targetRoleName_4 = e_2.getTargetRoleName();
            _builder.append(_targetRoleName_4, "		        ");
            _builder.append("\";");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t    ");
            _builder.append("    ");
            _builder.newLine();
            _builder.append("\t\t    ");
            _builder.append("    ");
            _builder.append("if (associatedExample.getId() != null) {");
            _builder.newLine();
            _builder.append("\t\t    ");
            _builder.append("        ");
            _builder.append("criteria.add(Restrictions.eq(\"");
            String _targetRoleName_5 = e_2.getTargetRoleName();
            _builder.append(_targetRoleName_5, "		            ");
            _builder.append(".id\", associatedExample.getId()));");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t    ");
            _builder.append("    ");
            _builder.append("} else {");
            _builder.newLine();
            _builder.append("\t\t    ");
            _builder.append("        ");
            _builder.append("criteria.createCriteria(");
            String _targetRoleName_6 = e_2.getTargetRoleName();
            _builder.append(_targetRoleName_6, "		            ");
            _builder.append("Path).add(parameters.createExample(associatedExample));");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t    ");
            _builder.append("    ");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t\t    ");
            _builder.append("    ");
            _builder.append("addedAssociations.add(associatedExample);");
            _builder.newLine();
            {
              BusinessClass _target_2 = e_2.getTarget();
              List<Association> _allToOneAssociationsInHierarchy_1 = BusinessClassHelpers.getAllToOneAssociationsInHierarchy(_target_2);
              int _size_1 = _allToOneAssociationsInHierarchy_1.size();
              boolean _operator_greaterThan_1 = ComparableExtensions.<Integer>operator_greaterThan(((Integer)_size_1), ((Integer)0));
              if (_operator_greaterThan_1) {
                _builder.append("\t\t    ");
                _builder.append("    ");
                BusinessClass _target_3 = e_2.getTarget();
                String _name_49 = _target_3.getName();
                String _javaDaoBaseClass_1 = NameMapper.javaDaoBaseClass(_name_49);
                _builder.append(_javaDaoBaseClass_1, "		        ");
                _builder.append(".addAssociatedExampleCriteria(criteria, associatedExample, ");
                String _targetRoleName_7 = e_2.getTargetRoleName();
                _builder.append(_targetRoleName_7, "		        ");
                _builder.append("Path + \".\", parameters, addedAssociations);");
                _builder.newLineIfNotEmpty();
              }
            }
            _builder.append("\t\t    ");
            _builder.append("}");
            _builder.newLine();
          }
        }
        {
          List<Association> _oneAssociationsFromInHierarchy_1 = BusinessClassHelpers.getToOneAssociationsFromInHierarchy(clazz);
          for(final Association e_3 : _oneAssociationsFromInHierarchy_1) {
            _builder.append("\t\t    ");
            _builder.append("if (example.get");
            String _sourceRoleName = e_3.getSourceRoleName();
            String _firstUpper_3 = StringExtensions.toFirstUpper(_sourceRoleName);
            _builder.append(_firstUpper_3, "		    ");
            _builder.append("() != null && !addedAssociations.contains(example.get");
            String _sourceRoleName_1 = e_3.getSourceRoleName();
            String _firstUpper_4 = StringExtensions.toFirstUpper(_sourceRoleName_1);
            _builder.append(_firstUpper_4, "		    ");
            _builder.append("())) {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t    ");
            _builder.append("    ");
            _builder.newLine();
            _builder.append("\t\t    ");
            _builder.append("    ");
            BusinessClass _source_1 = e_3.getSource();
            String _name_50 = _source_1.getName();
            String _javaDomainClass_34 = NameMapper.javaDomainClass(_name_50);
            _builder.append(_javaDomainClass_34, "		        ");
            _builder.append(" associatedExample = example.get");
            String _sourceRoleName_2 = e_3.getSourceRoleName();
            String _firstUpper_5 = StringExtensions.toFirstUpper(_sourceRoleName_2);
            _builder.append(_firstUpper_5, "		        ");
            _builder.append("();");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t    ");
            _builder.append("    ");
            _builder.append("String ");
            String _sourceRoleName_3 = e_3.getSourceRoleName();
            _builder.append(_sourceRoleName_3, "		        ");
            _builder.append("Path = associationPath + \"");
            String _sourceRoleName_4 = e_3.getSourceRoleName();
            _builder.append(_sourceRoleName_4, "		        ");
            _builder.append("\";");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t    ");
            _builder.append("    ");
            _builder.newLine();
            _builder.append("\t\t    ");
            _builder.append("    ");
            _builder.append("if (associatedExample.getId() != null) {");
            _builder.newLine();
            _builder.append("\t\t    ");
            _builder.append("        ");
            _builder.append("criteria.add(Restrictions.eq(\"");
            String _sourceRoleName_5 = e_3.getSourceRoleName();
            _builder.append(_sourceRoleName_5, "		            ");
            _builder.append(".id\", associatedExample.getId()));");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t    ");
            _builder.append("    ");
            _builder.append("} else {");
            _builder.newLine();
            _builder.append("\t\t    ");
            _builder.append("        ");
            _builder.append("criteria.createCriteria(");
            String _sourceRoleName_6 = e_3.getSourceRoleName();
            _builder.append(_sourceRoleName_6, "		            ");
            _builder.append("Path).add(parameters.createExample(associatedExample));");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t    ");
            _builder.append("    ");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t\t    ");
            _builder.append("    ");
            _builder.append("addedAssociations.add(associatedExample);");
            _builder.newLine();
            {
              BusinessClass _source_2 = e_3.getSource();
              List<Association> _allToOneAssociationsInHierarchy_2 = BusinessClassHelpers.getAllToOneAssociationsInHierarchy(_source_2);
              int _size_2 = _allToOneAssociationsInHierarchy_2.size();
              boolean _operator_greaterThan_2 = ComparableExtensions.<Integer>operator_greaterThan(((Integer)_size_2), ((Integer)0));
              if (_operator_greaterThan_2) {
                _builder.append("\t\t    ");
                _builder.append("    ");
                BusinessClass _source_3 = e_3.getSource();
                String _name_51 = _source_3.getName();
                String _javaDaoBaseClass_2 = NameMapper.javaDaoBaseClass(_name_51);
                _builder.append(_javaDaoBaseClass_2, "		        ");
                _builder.append(".addAssociatedExampleCriteria(criteria, associatedExample, ");
                String _sourceRoleName_7 = e_3.getSourceRoleName();
                _builder.append(_sourceRoleName_7, "		        ");
                _builder.append("Path + \".\", parameters, addedAssociations);");
                _builder.newLineIfNotEmpty();
              }
            }
            _builder.append("\t\t    ");
            _builder.append("}");
            _builder.newLine();
          }
        }
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
      }
    }
    _builder.append("\t");
    _builder.append("private static final class ");
    String _name_52 = clazz.getName();
    String _javaDomainClass_35 = NameMapper.javaDomainClass(_name_52);
    _builder.append(_javaDomainClass_35, "	");
    _builder.append("HibernateCallback implements HibernateCallback {");
    _builder.newLineIfNotEmpty();
    _builder.append("        ");
    _builder.newLine();
    _builder.append("\t    ");
    _builder.append("private final ");
    String _name_53 = clazz.getName();
    String _javaDomainClass_36 = NameMapper.javaDomainClass(_name_53);
    _builder.append(_javaDomainClass_36, "	    ");
    _builder.append(" example;");
    _builder.newLineIfNotEmpty();
    _builder.append("        ");
    _builder.append("private final SearchParameters parameters;");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("private final boolean rowcount;");
    _builder.newLine();
    _builder.newLine();
    _builder.append("        ");
    _builder.append("private ");
    String _name_54 = clazz.getName();
    String _javaDomainClass_37 = NameMapper.javaDomainClass(_name_54);
    _builder.append(_javaDomainClass_37, "        ");
    _builder.append("HibernateCallback(");
    String _name_55 = clazz.getName();
    String _javaDomainClass_38 = NameMapper.javaDomainClass(_name_55);
    _builder.append(_javaDomainClass_38, "        ");
    _builder.append(" example, SearchParameters parameters) {");
    _builder.newLineIfNotEmpty();
    _builder.append("            ");
    _builder.append("this.example = example;");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("this.parameters = parameters;");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("this.rowcount = false;");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("        ");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("private ");
    String _name_56 = clazz.getName();
    String _javaDomainClass_39 = NameMapper.javaDomainClass(_name_56);
    _builder.append(_javaDomainClass_39, "        ");
    _builder.append("HibernateCallback(");
    String _name_57 = clazz.getName();
    String _javaDomainClass_40 = NameMapper.javaDomainClass(_name_57);
    _builder.append(_javaDomainClass_40, "        ");
    _builder.append(" example, SearchParameters parameters, boolean rowcount) {");
    _builder.newLineIfNotEmpty();
    _builder.append("            ");
    _builder.append("this.example = example;");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("this.parameters = parameters;");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("this.rowcount = rowcount;");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("        ");
    _builder.append("public Object doInHibernate(final Session session) throws HibernateException, SQLException {");
    _builder.newLine();
    _builder.newLine();
    _builder.append("        \t");
    _builder.append("Criteria criteria = session.createCriteria(");
    String _name_58 = clazz.getName();
    String _javaDomainClass_41 = NameMapper.javaDomainClass(_name_58);
    _builder.append(_javaDomainClass_41, "        	");
    _builder.append(".class);");
    _builder.newLineIfNotEmpty();
    _builder.append("        \t");
    _builder.append("if (example.getId() != null) {");
    _builder.newLine();
    _builder.append("        \t\t");
    _builder.append("criteria.add(Restrictions");
    _builder.newLine();
    _builder.append("        \t\t\t\t");
    _builder.append(".eq(\"id\", example.getId()));");
    _builder.newLine();
    _builder.append("        \t");
    _builder.append("} else {");
    _builder.newLine();
    _builder.append("        \t\t");
    _builder.append("criteria.add(parameters.createExample(example));");
    _builder.newLine();
    _builder.append("        \t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("        \t");
    _builder.append("if (rowcount) {");
    _builder.newLine();
    _builder.append("        \t    ");
    _builder.append("criteria.setProjection(Projections.rowCount());");
    _builder.newLine();
    _builder.append("        \t");
    _builder.append("} else {");
    _builder.newLine();
    _builder.append("\t\t\t    ");
    _builder.append("criteria = parameters.constrainCriteria(criteria);");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("}");
    _builder.newLine();
    {
      List<Association> _allToOneAssociationsInHierarchy_3 = BusinessClassHelpers.getAllToOneAssociationsInHierarchy(clazz);
      int _size_3 = _allToOneAssociationsInHierarchy_3.size();
      boolean _operator_greaterThan_3 = ComparableExtensions.<Integer>operator_greaterThan(((Integer)_size_3), ((Integer)0));
      if (_operator_greaterThan_3) {
        _builder.append("\t\t\t");
        _builder.append("addAssociatedExampleCriteria(criteria, example, \"\", parameters, new HashSet<Object>());");
        _builder.newLine();
      }
    }
    _builder.append("        \t");
    _builder.append("return criteria.list();");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    return _builder;
  }
}